package service

import (
	"errors"
	"github.com/gin-gonic/gin"
	"mall-app/internal/dao"
	"time"
)

type loginService struct{}

// 系统登录
func UserLogin(username, password string, ctx *gin.Context) (string, error) {
	// 查询用户
	var user dao.User

	// SELECT * FROM users WHERE name = '';
	dao.DB.Where("user_name = ?", username).Find(&user)

	if user.Status != 1 {
		return "", errors.New("您的账号已被禁用,请联系管理员")
	}

	// 更新登录时间、登录IP
	utils.XormDb.Id(user.Id).Update(&model.User{LoginTime: time.Now().Unix(), LoginIp: utils.GetClientIp(ctx), UpdateTime: time.Now().Unix()})

	// 生成Token
	token, _ := utils.GenerateToken(user.Id, user.Username, user.Password)
	// 返回token
	return token, nil
}
